#pragma once
#include <functional>
#include <string>

#include "protocol.hpp"

using cal_t = std::function<Response(const Requst&)>;
class Parse
{
	public:
		Parse(cal_t cal):_cal(cal)
		{

		}
	std::string Entry(std::string& package)
	{
		std::string message;
		std::string respstr;
		while(RemoveHander(package,message))
		{
			// 完整的报文
			if(message.empty()) break;
			// 反序列化
			Requst req;
			if(!req.Deserialize(message))
			{
				break;
			}
			std::cout << "#############" << std::endl;
			req.Print();
			std::cout << "#############" << std::endl;
			// 计算
			Response res = _cal(req);
	
			// 序列化
			std::string str;
			res.Serialize(str);
			// 添加报头
			AddHander(str);
			// 拼接
			respstr += str;
	
			return respstr;
		}
		return std::string();
	}
private:
	cal_t _cal;
};